package com.aptemo.webetek.server.services;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.jdo.PersistenceManager;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.aptemo.webetek.server.CommonService;
import com.aptemo.webetek.server.data.PMF;
import com.aptemo.webetek.server.data.Pick;
import com.aptemo.webetek.server.data.Stake;
import com.aptemo.webetek.server.datastore.DataStoreAPI;
import com.aptemo.webetek.server.datastore.DataStoreHelper;
import com.aptemo.webetek.server.datastore.DataStoreImpl;
import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.KeyFactory;
import com.google.appengine.api.users.UserService;
import com.google.appengine.api.users.UserServiceFactory;
import com.google.gson.Gson;

/**
 * The server side implementation of the Invitation service.
 */
public class RemoveBetPickService
		extends HttpServlet
{

	/**
	 * 
	 */
	private static final long	serialVersionUID	= 2442498766498504653L;

	/**
	 * 
	 */
	private UserService			userService			= UserServiceFactory.getUserService();

	private DataStoreHelper		dataHelper			= new DataStoreHelper();

	private DataStoreAPI		dataService			= new DataStoreImpl();

	private CommonService		utils				= new CommonService();

	protected void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException
	{
		boolean writemode = false;
		int limit = 10;

		res.setContentType("text/html");
		PrintWriter out = res.getWriter();

		String url = req.getRequestURI().toString();
		String queryString = req.getQueryString();
		if (queryString != null)
		{
			url += "?" + queryString;
		}

		String stakeId = req.getParameter("stakeId");
		String pickId = req.getParameter("pickId");

		Key uk = KeyFactory.stringToKey(stakeId).getParent();
		String userId = KeyFactory.keyToString(uk);

		// 
		String loginUrl = userService.createLoginURL(url);
		if (!userService.isUserLoggedIn() && !userService.isUserAdmin())
		{
			res.sendRedirect(loginUrl);
			return;
		}
		
		// 
		String redirect = null;
		PersistenceManager pm = PMF.get().getPersistenceManager();
		try
		{
			String content = "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">";
			content += "<html><head><title>Online betting with Webetek</title></head>";
			content += "<body>";

			content += "User: " + dataService.getPlayer(userId).getEmail() + "<br/>";

			Key skey = KeyFactory.stringToKey(stakeId);
			Stake stake = pm.getObjectById(Stake.class, skey);

			Key pkey = KeyFactory.stringToKey(pickId);
			Pick pick = pm.getObjectById(Pick.class, pkey);

			List<Key> picks = stake.getPickKeys();

			content += "Original bet picks size: " + picks.size() + "<br/>";

			picks.remove(pick.getId());

			stake.setPickKeys(picks);

			content += "Pick '" + pick.getTitle() + "' (" + pick.getSid() + ") removed!<br/>";
			pm.makePersistent(stake);

			content += "New bet picks size: " + stake.getPickKeys().size() + "<br/>";

			content += "</body>";
			content += "</html>";

			out.println(content);
		}
		catch (Exception e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		finally
		{
			out.close();
			pm.close();
		}

		// TODO....
		// res.sendRedirect("error");
	}
}
